﻿using ProjectFootball.MVC.Areas.VenueArea.Models;
using ProjectFootball.MVC.Foundation;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using ProjectFootball.MVC.Models.EntityFramework.Entities;
using ProjectFootball.MVC.Models.RepositoryProviders;
using System;
using System.Linq;
using System.Security.Principal;
using System.Web.Mvc;

namespace ProjectFootball.MVC.Areas.VenueArea.Controllers
{
    [Authorize]
    public class EditorController : BaseController
    {
        #region Constructor
        public EditorController(IRepositoryProvider provider)
            :base(provider)
        {
        }
        #endregion

        #region /VenueArea/Editor
        public ActionResult Index(Guid id)
        {
            var venue = Provider.VenueRepository.SearchByPrimaryKey(id);

            if (venue == null)
                return RedirectHome;

            return View(venue.GetDataModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(VenueDataModel venueDataModel)
        {
            if (!ModelState.IsValid)
                return View(venueDataModel);

            var venue = Provider.VenueRepository.SearchByPrimaryKey(venueDataModel.VenueGuid);

            if (venue.CanEdit(User))
                venue.Edit(venueDataModel, User);
            else
            {
                venue = venueDataModel.CreateLinked(User);
                Provider.VenueRepository.Add(venue);
            }

            Provider.SaveChanges();

            return RedirectToAction("Index", "Details", new { id = venue.Pk });
        }
        #endregion

        #region /VenueArea/Editor/CreateNew
        public ActionResult CreateNew()
        {
            return View("Index", new VenueDataModel());
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateNew(VenueDataModel venueDataModel)
        {
            if (!ModelState.IsValid)
                return View("Index", venueDataModel);

            var venue = venueDataModel.CreateNew(User);

            Provider.VenueRepository.Add(venue);

            Provider.SaveChanges();
            return RedirectToAction("Index", "Details", new { id = venue.Pk });
        }
        #endregion

        #region /VenueArea/Editor/Delete
        public ActionResult Delete(Guid id)
        {
            var venue = Provider.VenueRepository.SearchByPrimaryKey(id);

            if (venue == null || !venue.CanDelete(User))
                return RedirectHome;

            Provider.VenueRepository.Remove(venue);
            Provider.SaveChanges();

            return RedirectHome;
        }
        #endregion

        #region /VenueArea/Editor/RecordForApproval
        [Authorize(Roles = "Admin, Superuser")]
        public ActionResult RecordForApproval(Guid id)
        {
            var venue = Provider.VenueRepository.SearchByPrimaryKey(id);
            var venues = Provider.VenueRepository.Where(c => c.ApprovalGuid == venue.ApprovalGuid);

            return View("Approval", venues.Search(User, SearchType.RecordForApproval));
        }
        #endregion

        #region /Venue/Editor/ConfirmApproval
        [Authorize(Roles = "Admin, Superuser")]
        public ActionResult ConfirmApproval(Guid id)
        {
            if (!User.IsApprover())
                return RedirectHome;

            var venue = Provider.VenueRepository.SearchByPrimaryKey(id);

            if (venue == null || !venue.CanApprove(User))
                return RedirectHome;

            var venueQuery = from v in Provider.VenueRepository.Where(v => v.ApprovalGuid == venue.ApprovalGuid && v.IsApproved)
                             select v;

            Guid approvedVenueGuid;

            if (venueQuery.Any())
            {
                var approvedVenue = venueQuery.Single();

                approvedVenueGuid = approvedVenue.Pk;
                venue.Approve(approvedVenue);

                Provider.SaveChanges();
            }
            else
            {
                venue.IsApproved = true;
                approvedVenueGuid = venue.Pk;
                Provider.SaveChanges();
            }

            var venues = Provider.VenueRepository.Where(c => c.ApprovalGuid == venue.ApprovalGuid && !c.IsApproved);
            Provider.VenueRepository.RemoveAll(venues);

            Provider.SaveChanges();

            return RedirectToAction("Index", "Details", new { id = approvedVenueGuid });
        }
        #endregion

        #region CountryArea/Editor/PendingApprovals
        [Authorize(Roles = "Admin, Superuser")]
        public ActionResult PendingApprovals()
        {
            var venues = Provider.VenueRepository.Where(c => c.IsApproved == false);

            return View("Approval", venues.Search(User, SearchType.PendingApprovals));
        }
        #endregion
    }
}
